package com.whfc.ms.api.fmam.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fmam.entity.dto.*;
import com.whfc.fmam.entity.qo.RecvSendAnalysisQO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fmam.service.MatAnalysisService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Author: ；likang
 * @Description: 物资统计分析
 * @Date:Create：in 2019/12/9 11:09
 * @Version：1.0
 */
@RequiresPermissions("fmam/ana")
@RestController
@RequestMapping("/ms/api/fmam/analysis")
public class FmamAnalysisController extends BaseController {

    @Autowired
    private MatAnalysisService matAnalysisServiceImpl;


    /**
     * 供应商供货明细列表服务
     *
     * @param user, request
     * @return
     */
    @PostMapping("/supplier/list")
    public Result supplierList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestBody @Validated RecvSendAnalysisQO request) {
        logger.info("供应商供货明细列表，loginer:{},WeighNoteListQO:{}", user.getUsername(), request.toString());
        FmamSendDetailAnaDTO fmamSendDetailAnaDTO = matAnalysisServiceImpl.sendSupplierList(request);
        return ResultUtil.success(fmamSendDetailAnaDTO);
    }

    /**
     * 供应商偏差情况分析超负差率列表服务
     *
     * @param user, request
     * @return
     */
    @PostMapping("/supplier/diffNum")
    public Result supplierDiffNumRank(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                      @RequestBody @Validated RecvSendAnalysisQO request) {
        logger.info("供应商供货偏差情况分析超负差数列表，loginer:{},WeighNoteListQO:{}", user.getUsername(), request.toString());
        List<FmamSendSupplierDTO> fmamSendSupplierDTOS = matAnalysisServiceImpl.supplierDiffNumRank(request);
        return ResultUtil.success(fmamSendSupplierDTOS);
    }

    /**
     * 供应商偏差情况分析超负差率列表服务
     *
     * @param user, request
     * @return
     */
    @PostMapping("/supplier/diffRate")
    public Result supplierDiffRateRank(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                       @RequestBody @Validated RecvSendAnalysisQO request) {
        logger.info("供应商供货偏差情况分析超负差率列表，loginer:{},WeighNoteListQO:{}", user.getUsername(), request.toString());
        List<FmamSendSupplierDTO> fmamSendSupplierDTOS = matAnalysisServiceImpl.supplierDiffRateRank(request);
        return ResultUtil.success(fmamSendSupplierDTOS);
    }


    @RequestMapping("/mat")
    public Result analysisByMat(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestBody @Validated RecvSendAnalysisQO request) {
        logger.info("按材料明细进行收料和发料汇总分析,loginer:{},RecvSendAnalysisQO:{}", user.getUsername(), request.toString());
        FmamRecvSendAnaDTO fmamRecvSendAnaByMatDTO = matAnalysisServiceImpl.analysisByMat(user, request);
        return ResultUtil.success(fmamRecvSendAnaByMatDTO);
    }

    @GetMapping("/mat/getWeighNoteList")
    public Result getWeighNoteList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                   @RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam("type") Integer type,
                                   @RequestParam("matId") Long matId,
                                   @RequestParam(value = "diffResult", required = false) Integer diffResult) {
        logger.info("收发料汇总分析之根据材料id查找磅单列表,loginer:{},pageNum:{},pageSize:{},matId:{},type:{},diffResult:{}",
                user.getUsername(), pageNum, pageSize, matId, type, diffResult);
        PageData<FmamRecvSendAnaMatDTO> pageData = matAnalysisServiceImpl.getWeighNoteListByMatId(pageNum, pageSize, matId, type, diffResult);
        return ResultUtil.success(pageData);
    }

    @RequestMapping("/matType")
    public Result analysisByMatType(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @RequestBody @Validated RecvSendAnalysisQO request) {
        logger.info("按材料类别进行收料和发料汇总分析,loginer:{},RecvSendAnalysisQO:{}", user.getUsername(), request.toString());
        FmamRecvSendAnaDTO fmamRecvSendAnaByMatDTO = matAnalysisServiceImpl.analysisByMatType(request);
        return ResultUtil.success(fmamRecvSendAnaByMatDTO);
    }

    @GetMapping("/matType/getWeighNoteList")
    public Result getWeighNoteListByMatType(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                            @RequestParam("pageNum") Integer pageNum,
                                            @RequestParam("pageSize") Integer pageSize,
                                            @RequestParam("type") Integer type,
                                            @RequestParam("matTypeId") Long matTypeId,
                                            @RequestParam(value = "diffResult", required = false) Integer diffResult) {
        logger.info("收发料汇总分析之根据材料类别查找磅单列表,loginer:{},pageNum:{},pageSize:{},matTypeId:{},type:{},diffResult:{}",
                user.getUsername(), pageNum, pageSize, matTypeId, type, diffResult);
        PageData<FmamRecvSendAnaMatDTO> pageData = matAnalysisServiceImpl.getWeighNoteListByMatType(pageNum, pageSize, matTypeId, type, diffResult);
        return ResultUtil.success(pageData);
    }

    @RequestMapping("/supplier/group")
    public Result supplierGroup(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestBody @Validated RecvSendAnalysisQO request) {
        logger.info("供应商偏差分析按供应商分组,loginer:{},RecvSendAnalysisQO:{}", user.getUsername(), request.toString());
        List<FmamSendSupplierDTO> fmamSendSupplierDTOS = matAnalysisServiceImpl.analysisBySupplier(request);
        return ResultUtil.success(fmamSendSupplierDTOS);
    }


    @PostMapping("/supplier/matGroup")
    public Result matGroup(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestBody @Validated RecvSendAnalysisQO request) {
        logger.info("供应商供货偏差情况分析,按材料明细分组，loginer:{},param:{}",
                user.getUsername(), request.toString());
        ListData<FmamSupplierAnaMatDTO> listData = matAnalysisServiceImpl.supplierAnaByMatGroup(request);
        return ResultUtil.success(listData);
    }

    @RequestMapping("/supplier/diffData")
    public Result supplierDiffData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                   @RequestBody @Validated RecvSendAnalysisQO request) {
        logger.info("供应商偏差分析数据统计分析,loginer:{},RecvSendAnalysisQO:{}", user.getUsername(), request.toString());
        FmamRecvSendAnaDTO fmamRecvSendAnaDTO = matAnalysisServiceImpl.analysisDiffData(request);
        return ResultUtil.success(fmamRecvSendAnaDTO);
    }

    @GetMapping("/supplier/getWeighNoteList")
    public Result getWeighNoteListBySupplier(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                             @RequestParam("sendSupplierId") Integer sendSupplierId,
                                             @RequestParam("pageNum") Integer pageNum,
                                             @RequestParam("pageSize") Integer pageSize,
                                             @RequestParam(value = "diffResult", required = false) Integer diffResult) {
        logger.info("根据供应商id查找磅单列表,loginer:{},sendSupplierId:{},pageNum:{},pageSize:{},diffResult:{}",
                user.getUsername(), sendSupplierId, pageNum, pageSize, diffResult);
        PageData<FmamRecvSendAnaMatDTO> pageData = matAnalysisServiceImpl.getWeighNoteListBySupplier(sendSupplierId, pageNum, pageSize, diffResult);
        return ResultUtil.success(pageData);
    }


    @PostMapping("/mat/export")
    public Result exportAnalysisByMatData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                          @RequestBody @Validated RecvSendAnalysisQO request) throws IOException {
        logger.info("导出收发料汇总分析数据,loginer:{},param:{}", user.getUsername(), request.toString());
        OssPathDTO path = matAnalysisServiceImpl.exportAnalysisByMatData(request);
        return ResultUtil.success(path);
    }

    @PostMapping("/supplier/export")
    public Result exportAnalysisBySupplierData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                               @RequestBody @Validated RecvSendAnalysisQO request) throws IOException {
        logger.info("导出供应商偏差分析数据:loginer:{},param:{}", user.getUsername(), request.toString());
        OssPathDTO path = matAnalysisServiceImpl.exportAnalysisBySupplierData(request);
        return ResultUtil.success(path);
    }


}
